/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import global.*;
import java.io.ByteArrayOutputStream;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import obj.NguoiDung;
import view.ShowAlert;

/**
 *
 * @author lenovo
 */
public class KiemTraDangNhap {
    
    public static boolean ServerResponse(HttpConnection http, InputStream iStrm) throws IOException
    {
        int iRes = -1;
        boolean bRes = false;
        if (http.getResponseCode() == HttpConnection.HTTP_OK)
        {
              int length = (int) http.getLength();
              if (length != -1)
              {
                    byte servletData[] = new byte[length];
                    iStrm.read(servletData);
                    Global.sServerRespon = new String(servletData);
              }
              else
              {
                    ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
                    int ch;
                    while ((ch = iStrm.read()) != -1)
                      bStrm.write(ch);
                    Global.sServerRespon = new String(bStrm.toByteArray());
                    bStrm.close();
              }
              iRes = 0;
              bRes = true;
        }
        return  bRes;
    }
    
    public static int DangNhap(String sUsername, String sPassword, Form frm, Display display)
    {
        HttpConnection http = null;
        InputStream iStrm = null;  
        boolean bRes = false;
        int iRes = -1;

        String url = ServerInfor.url + "/mobile/MobileLogin.aspx"  + "?" +
                 "sUserName=" + sUsername + "&" + 
                 "sPassword=" + sPassword;

        try
        {
          http = (HttpConnection) Connector.open(url);
          http.setRequestMethod(HttpConnection.GET);
          iStrm = http.openInputStream();      

          bRes = ProcessServerResponse.ServerResponse(http, iStrm);
          if(bRes == true)
          {
              if(Global.sServerRespon.equals("false") || Global.sServerRespon.equals(null))
              {
                  iRes = 0;
              }
              else
              {
                   CurrentUserInfor.nguoiDungDanngNhap = new NguoiDung();
                   CurrentUserInfor.nguoiDungDanngNhap.fromJSON(Global.sServerRespon);
                   iRes = 1;
              }
          }
        }
        catch(IOException ex)
        {
            ShowAlert.ShowAlert(display, frm, "Lỗi kết nối", "Không  thể kết nối với Server", 3);
        }
        finally
        {
            try
            {
              if (iStrm != null)
                iStrm.close();
              if (http != null)
                http.close();
            }
            catch(IOException ex){}
        }
        return iRes;
    }
    

//    public static int DangNhapDung(String sUsername, String sPassword, Form frm, Display display)
//    {
//        String strServer = new String("");
//        HttpConnection http = null;
//        OutputStream oStrm = null;
//        InputStream iStrm = null;
//        
//        int iRes = 0;
//
//        String url = ServerInfor.url + "/Account/MobileLogin.aspx";
//
//        try
//        {
//          http = (HttpConnection) Connector.open(url);
//          oStrm = http.openOutputStream();
//
//          http.setRequestMethod(HttpConnection.POST);
//          http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//          byte data[] = ("sUserName=" + sUsername).getBytes();
//          oStrm.write(data);
//          data = ("&sPassword=" + sPassword).getBytes();
//          oStrm.write(data);
//
//          iStrm = http.openInputStream();       
//          iRes = ProcessServerResponse.ServerResponse(http, iStrm, strServer);
//          if(iRes == 0)
//          {
//              if(! strServer.equals("fasle"))
//              {
//                   CurrentUserInfor.nguoiDungDanngNhap = new NguoiDung();
//                   CurrentUserInfor.nguoiDungDanngNhap.fromJSON(strServer);
//                   iRes = 1;
//              }
//          }
//          else
//          {
//              iRes = -1;
//          }
//        }
//        catch(IOException ioex)
//        {
//            iRes = -1;
//            ioex.printStackTrace();         
//        }
//        finally
//        {
//            try
//              {
//                  if (iStrm != null)
//                    iStrm.close();
//                  if (oStrm != null)
//                    oStrm.close();        
//                  if (http != null)
//                    http.close();
//              }
//              catch(IOException ex)
//              {
//                  iRes = -1;
//                  ex.printStackTrace();
//              }
//        }  
//        return iRes;
//    }
}

